import { BrowserWindow } from 'electron';
import { loadWindowUrl } from '$main/utils/window';
import { getBaseConfig } from './config';
import { RcOpenChildWindow } from '#common/types/event';
import { join } from 'node:path';
import { loadDevtools } from '$main/internal/devtool';
import { centerDisplayPoint } from '$main/utils/screen';

function createChildWindow(parent: BrowserWindow, param: RcOpenChildWindow) {
  const centerPoint = centerDisplayPoint(param.width, param.height);
  let childWin: BrowserWindow | null = new BrowserWindow(
    getBaseConfig({
      title: param.title,
      width: param.width,
      height: param.height,
      x: centerPoint.x,
      y: centerPoint.y,
      zoomToPageWidth: true,
      parent: parent,
      resizable: false,
      frame: false,
      titleBarStyle: 'default',
      webPreferences: {
        preload: join(__dirname, '../preload/index.js'),
        nodeIntegration: false
        //contextIsolation: true
      }
    })
  );
  loadDevtools();

  loadWindowUrl(childWin, param.url);

  childWin.on('closed', () => {
    childWin = null;
  });
}

export { createChildWindow };
